@echo off

echo %~dp0  
set ROOT_DIR=%~dp0

set input=%1%
set prj_path=MM32F527x
set prj_name=MM32F527x

if "%input%"=="clean" (
    goto clean
) else if "%input%"=="help" (
    echo "=================================================="
    echo "                                                  "                                                                                          
    echo "-----------Show How to Build Project--------------"
    echo "                                                  "  
    echo "**************************************************"
    echo "       Keilbuild.bat clean                        "
    echo "       clean default project for default board    "
    echo "**************************************************"
    goto end
)

if exist %ROOT_DIR%\output (
    echo output dir exist
    del /s /Q output\
) else (
    echo output dir creating
    md %ROOT_DIR%\output
)

rem 设置编译器和项目工程文件路径
set UV="C:\Keil_v5\UV4\UV4.exe"
set UV_PRJ_PATH="%ROOT_DIR%\Bsp\%prj_path%\Project\%prj_name%.uvprojx"

echo Init building ...

echo Keil Tool PATH : %UV%
echo Project PATH   : %UV_PRJ_PATH%

echo Start build lwip project ...
:: echo .>build_log.txt
%UV% -j0 -r %UV_PRJ_PATH% -o %ROOT_DIR%\output\build_lwip_log.txt
:: type build_log.txt
echo Copy %prj_name%.hex
copy %ROOT_DIR%\Bsp\%prj_path%\Project\Objects\%prj_name%.hex .\output\
::copy %ROOT_DIR%\%prj_path%\bin\%prj_name%.bin .\output\

:clean

echo Clean Project
rem 删除目录下的文件以及文件夹
del /s /Q %ROOT_DIR%\Bsp\%prj_path%\Project\Objects\
del /s /Q %ROOT_DIR%\Bsp\%prj_path%\Project\Listings\
del /s /Q %ROOT_DIR%\Bsp\%prj_path%\Project\%prj_name%.uvguix.lsslny

rem 删除文件夹
::rd %ROOT_DIR%\output
rd %ROOT_DIR%\Bsp\%prj_path%\Project\Objects
::rd %ROOT_DIR%\%prj_path%\DebugConfig
rd %ROOT_DIR%\Bsp\%prj_path%\Project\Listings

rem 输出编译信息
type %ROOT_DIR%\output\build_lwip_log.txt

:end

echo Done.
pause
